package net.liteheaven.dtimer;

import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by LiangYu on 2018/8/3.
 */
public class XLog {

    private static final String TAG = "DTimer";
    // 启动就一直开着
    private static File file;

    static {
        File fPub = Environment.getExternalStorageDirectory();
        String path = fPub.getAbsolutePath() + "/" + " dtlog.txt";
        file = new File(path);
        if (file.exists()){
            file.delete();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void i(String text){
        Log.i(TAG, text);
        Calendar calendar = Calendar.getInstance();
        String timeStr = new SimpleDateFormat("HH:mm:ss.SSS").format(calendar.getTime());
        writeLog(timeStr + " " + text + "\n");
    }

    private static void writeLog(String text) {
        try {
            FileOutputStream os = new FileOutputStream(file, true);
            byte [] va = text.getBytes();
            os.write(va);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
